<?php

namespace Home\Controller;

/**
 * 会员管理
 *
 * @author Administrator
 *        
 */
class UserController extends BaseController {
	
	/**
	 * 获取用户列表
	 */
	public function mb_GetUserList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$userId = $data->userid;
		$adminCode = $data->admincode;
		
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		
		if ($userId != '') {
			$where = "id = '$userId'";
		} else if ($date1 != '' && $date2 != '') {
			$where = "regist>='$date1' AND regist<='$date2'";
		} else {
			$where = "";
		}
		
		if ($adminCode == '') {
			$where .= "";
		} else {
			if ($where != '')
				$where .= ' AND ';
			$where .= "code = '$adminCode'";
		}
		
		if ($adminData ['level'] == 0) {
			// 总管理员
			$where .= '';
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			// $this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			// return;
			$where .= '';
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			if ($where != '')
				$where .= ' AND ';
			$where .= "code like '" . $subCode . "___'";
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$code = $adminData ['code'];
			if ($where != '')
				$where .= ' AND ';
			$where .= "code ='$code'";
		}
		
		$userData = $userInfo->where ( $where )->order ( 'regist' )->select ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 更新用户状态
	 */
	public function mb_UpdateUserStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		$status = $data->status;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		
		$userInfo = M ( 'Userinfo' );
		$userInfo->where ( "id='$id'" )->save ( array (
				'status' => $status 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 */
	public function mb_GetOnlineUsers() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$code = $adminData ['code'];
		
		$where = "";
		$userIds = json_decode ( \WMHandler::mb_GetInstance ()->mb_GetOnlineUserIds () );
		for($i = 0; $i < count ( $userIds ); $i ++) {
			$id = $userIds [$i];
			$where .= "id='$id'";
			if ($i != count ( $userIds ) - 1) {
				$where .= ' OR ';
			}
		}
		
		if ($adminData ['level'] == 0) {
			// 总管理员
			$where .= '';
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			if ($where != '') {
				$code = $adminData ['code'];
				$subCode = mb_substr ( $code, 0, 5 );
				$where .= ' AND ';
				$where .= "code like '" . $subCode . "___'";
			}
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			if ($where != '') {
				$where .= ' AND ';
				$where .= "code='$code'";
			}
		}
		$userInfo = M ( 'Userinfo' );
		if ($where != '')
			$userData = $userInfo->where ( $where )->select ();
		else
			$userData = array ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 */
	public function mb_ForceUserLogout() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$msg = \WMHandler::mb_GetInstance ()->mb_ForcedOffline ( $name );
		$this->mb_OutMessage ( ErrCode_Normal, $msg );
	}
	/**
	 * 修改密码
	 */
	public function mb_ResetPassword() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		$pass = $data->pass;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		$userInfo->where ( "name='$name'" )->save ( array (
				'basepass' => $pass 
		) );
		
		\WMHandler::mb_GetInstance ()->mb_UpdateUserPass ( $name, $pass );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
}